package com.zygk.locationtool.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.zygk.library.util.ToastUtil;
import com.zygk.locationtool.R;
import com.zygk.locationtool.activity.MapActivity;
import com.zygk.locationtool.adapter.StationListParentAdapter;
import com.zygk.locationtool.config.Constants;
import com.zygk.locationtool.dao.StationLogic;
import com.zygk.locationtool.model.M_Station;
import com.zygk.locationtool.model.M_Town;
import com.zygk.locationtool.model.apimodel.APIM_TownList;
import com.zygk.locationtool.util.HttpRequest;
import com.zygk.library.base.LazyFragment;
import com.zygk.library.util.StringUtils;
import com.zygk.library.view.SmoothListView.SmoothListView;
import java.util.ArrayList;
import java.util.List;

public class StationListInnerFragment extends LazyFragment
    implements SmoothListView.ISmoothListViewListener {

  Unbinder unbinder;
  @BindView(R.id.tv_no_data) TextView tvNoData;
  @BindView(R.id.rl_no_data) RelativeLayout rlNoData;
  @BindView(R.id.lv) SmoothListView lv;

  private StationListParentAdapter parentAdapter;
  private List<M_Town> siteParentList = new ArrayList<>();
  private int pagePos;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_site_list, null);
    unbinder = ButterKnife.bind(this, v);

    return v;
  }

  @Override protected void init() {
    registerReceiver(new String[] { Constants.BROADCAST_REFRESH_STATION_LIST });
    initData();
    initListener();
  }

  private void initData() {
    pagePos = getArguments().getInt("page_position");
    parentAdapter = new StationListParentAdapter(mContext, new ArrayList<M_Town>());
    parentAdapter.setPagePos(pagePos);
    lv.setAdapter(parentAdapter);
  }

  private void initListener() {
    lv.setSmoothListViewListener(this);
    lv.setRefreshEnable(true);
    lv.setLoadMoreEnable(false);

    parentAdapter.setOnItemClickListener(new StationListParentAdapter.onItemClickListener() {
      @Override public void onItemClick(M_Station station) {
        Intent intent = new Intent(mContext, MapActivity.class);
        intent.putExtra(MapActivity.INTENT_STATION, station);
        startActivity(intent);
      }
    });
  }

  @Override protected void lazyLoad() {
    if (siteParentList.isEmpty()) {
      showPd();
      station_list();
    }
  }

  private void station_list() {
    StationLogic.getTownStationList(mContext, pagePos + 1, new HttpRequest.HttpCallback() {
      @Override public void onStart() {
      }

      @Override public void onSucceed(Object obj) {
        APIM_TownList res = (APIM_TownList) obj;
        List<M_Town> townList = res.getResultObj();
        siteParentList.addAll(townList);
        fillAdapter(townList);
      }

      @Override public void onFailed() {
        ToastUtil.showNetErrorMessage(mContext);
      }

      @Override public void onFinish() {
        dismissPd();
        lv.stopRefresh();
      }
    });
  }

  // 填充数据
  private void fillAdapter(List<M_Town> list) {
    if (list == null || list.size() == 0) {
      rlNoData.setVisibility(View.VISIBLE);
      lv.setVisibility(View.GONE);
    } else {
      rlNoData.setVisibility(View.GONE);
      lv.setVisibility(View.VISIBLE);
      parentAdapter.setData(list);
    }
  }

  @Override public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }

  @Override public void onRefresh() {
    station_list();
  }

  @Override public void onLoadMore() {

  }

  @Override protected void handleReceiver(Context context, Intent intent) {
    if (intent == null || StringUtils.isBlank(intent.getAction())) {
      return;
    }
    if (Constants.BROADCAST_REFRESH_STATION_LIST.equals(intent.getAction())) {
      station_list();
    }
  }
}
